<?php
/**
 * ============================================================================
 * 版权所有 2008-2011 多多网络，并保留所有权利。
 * 网站地址: http://soft.duoduo123.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
session_start();
error_reporting(0);
include_once 'comm/config.php';
include_once 'comm/Taoapi.php';
include_once 'comm/page.class.php';
include_once 'comm/checkpostandget.php';

if(isset($_GET['cid'])){
	$cid=$_GET['cid'];
    if(!is_numeric($cid)){
		if($cid!='') $_GET['keyword']=$cid;
	    $cid='';
    }
	if($cid==0) $cid='';
}
else{
    $cid='';
}
$start_level=empty($_GET['start_level'])?'':(int)$_GET['start_level'];
$end_level=empty($_GET['end_level'])?'':(int)$_GET['end_level'];
$keyword=trim($_GET['keyword']);
$shopname=$keyword;

$area=$_GET['area'];
$cpr=$_GET['cpr'];
$type=$_GET['type'];
$px=$_GET['px'];
$subsql='1=1';
$butie=0;

if($cid!=''){
	$arrcid=preg_split("/[,]/", $cid);
	$subsql="(";
	for($i=0;$i<count($arrcid);$i++)
	{
		if($i==(count($arrcid)-1)){
			$subsql=$subsql." cid='".$arrcid[$i]."'";
		}else{
			$subsql=$subsql." cid='".$arrcid[$i]."' or ";
		}
	}
	$subsql=$subsql.")";
}

if($start_level!=''&&$end_level=='')
{
	$startlevel=$start_level;
	$endlevel=20;
}elseif($start_level==''&&$end_level!='')
{
	$startlevel=1;
	$endlevel=$end_level;
}
else
{
	$startlevel=$start_level;
	$endlevel=$end_level;
}
if($startlevel>$endlevel)
{
	$temp=$startlevel;
	$startlevel=$endlevel;
	$endlevel=$temp;
}

if($startlevel!='')
{
	$subsql=$subsql." and level>='".$startlevel."'";
}
if($endlevel!='')
{
	$subsql=$subsql." and level<='".$endlevel."'";
}
if($keyword!='请输入掌柜名称...'&&$keyword!='')
{
	$subsql=$subsql." and (nick like '%".$keyword."%' or title like '%".$keyword."%')";
}
else{
    $keyword='';
}
if($area!=''){
	$sql_area=str_replace(',','\',\'',$area);
	$subsql=$subsql." and (city in ('".$sql_area."') or state in ('".$sql_area."'))";
}
//if($cpr=='1')
//{
//	$subsql=$subsql." and consumer_protection='true'";
//}
if($type=='1')
{
	$subsql=$subsql." and `type`='B'";
}
if($px=='1'){
	$subsql=$subsql." order by level desc";
}
if($px=='0'){
	$subsql=$subsql." order by level asc";
}
if($px==''){
	$subsql=$subsql." order by tuijian desc,Id desc";
}
$page = !($_GET['page'])?'1':intval($_GET['page']);
$pagesize=20;
$frmnum=($page-1)*$pagesize;

$sql="select count(*) as totalnum from ".$BIAOTOU."shop where good>0 and fanxianlv>0 and ".$subsql;
$rsnum = mysql_query($sql);
$rownum = mysql_fetch_array($rsnum);
$total=$rownum["totalnum"];

$sql="select * from ".$BIAOTOU."shop where good>0 and fanxianlv>0 and ".$subsql." limit $frmnum,$pagesize";
$rs=mysql_query($sql);
$i=0;
include_once 'comm/wjt.php';
while ($row = mysql_fetch_array($rs)) {
	$shop[$i]['pic']="http://logo.taobaocdn.com/shop-logo".$row["pic_path"];
	$shop[$i]['type']=$row["type"];
	$shop[$i]['fanxianlv']=$row["fanxianlv"];
	$shop[$i]['title']=$row["title"];
	$shop[$i]['sid']=$row["sid"];
	$shop[$i]['nick']=$row["nick"];
	$shop[$i]['good']=$row["good"];
	$shop[$i]['level']=$row["level"];
	$shop[$i]['state']=$row["state"];
	$shop[$i]['city']=$row["city"];
	if($row["type"]=="B"){
		$shop[$i]['onerror']="images/tbsc.gif";
		if($row["pic_path"]=='Array'){
		    $shop[$i]['pic']=$shop[$i]['onerror'];
	    }
	}
	if($row["type"]=="C"){
		$shop[$i]['onerror']="images/tbdp.gif";
		if($row["pic_path"]=='Array'){
		    $shop[$i]['pic']=$shop[$i]['onerror'];
	    }
	}
	$shop[$i]['gourl']="goto.php?act=shop&url=&pic=".base64_encode($shop[$i]['pic'])."&fan=".$shop[$i]['fanxianlv']."&name=".urlencode($shop[$i]['title'])."&sid=".$shop[$i]['sid'];
	$i++;
}

if ($i == 0) {
	$nick = $keyword;
	$Taoapi_Config = Taoapi_Config :: Init();
	$Taoapi_Config->setCharset('UTF-8');
	$Taoapi = new Taoapi;
	$Taoapi->method = 'taobao.user.get';
	$Taoapi->fields = 'user_id,nick,consumer_protection,seller_credit,location,type';
	$Taoapi->nick = $nick;
	$TaoapiUsers = $Taoapi->Send('get', 'xml')->getArrayData();
	$Result_users = $TaoapiUsers["user"];
	$uid = $Result_users["user_id"];
	$nick = $Result_users["nick"];
	$shoptype = $Result_users["type"];
	$shoplevel = $Result_users["seller_credit"]["level"];
	$consumer_protection = $Result_users["consumer_protection"];
	$shopstate = $Result_users["location"]["state"];
	$shopcity = $Result_users["location"]["city"];
	$total_num = $Result_users["seller_credit"]['total_num'];
	$good_num = $Result_users["seller_credit"]['good_num'];
	$shopscore = $Result_users["seller_credit"]["score"];
	$good = round($good_num / $total_num * 100, 2);

	if ($nick != '' && $total_num > 0) {
		$Taoapi->method = 'taobao.shop.get';
		$Taoapi->fields = 'sid,cid,title,pic_path,created,shop_score';
		$Taoapi->nick = $nick;
		$ShopData = $Taoapi->Send('get', 'xml')->getArrayData();
		$Result_shop = $ShopData['shop'];
		$shoptitle = $Result_shop['title'];
		$item_score = $Result_shop["shop_score"]["item_score"];
		$service_score = $Result_shop["shop_score"]["service_score"];
		$delivery_score = $Result_shop["shop_score"]["delivery_score"];
		$shopcreated = $Result_shop['created'];
		$sid = $Result_shop['sid'];
		$shoppic = $Result_shop['pic_path'];

		$Taoapi->method = 'taobao.taobaoke.shops.convert';
		$Taoapi->fields = 'commission_rate,click_url';
		$Taoapi->sids = $Result_shop['sid'];
		$Taoapi->nick = MYUSERNICK;
		$ShopComm = $Taoapi->Send('get', 'xml')->getArrayData();
		$fanxianlv = $ShopComm['taobaoke_shops']['taobaoke_shop']['commission_rate'];
		$shop_click_url = $ShopComm['taobaoke_shops']['taobaoke_shop']['click_url'] ? $ShopComm['taobaoke_shops']['taobaoke_shop']['click_url'] : 'http://shop' . $sid . '.taobao.com/';
	}

	if ($sid > 0) {
		$shop[0]['pic'] = "http://logo.taobaocdn.com/shop-logo" . $shoppic;
		$shop[0]['type'] = $shoptype;
		$shop[0]['fanxianlv'] = $fanxianlv ? $fanxianlv : 0;
		$shop[0]['title'] = $shoptitle;
		$shop[0]['sid'] = $sid;
		$shop[0]['nick'] = $nick;
		$shop[0]['good'] = $good;
		$shop[0]['level'] = $shoplevel;
		$shop[0]['state'] = $shopstate;
		$shop[0]['city'] = $shopcity;
		if ($shoptype == "B") {
			$shop[0]['onerror'] = "images/tbsc.gif";
			if (is_array($shoppic)) {
				$shop[0]['pic'] = $shop[0]['onerror'];
			}
			if($shop_click_url=='http://shop' . $sid . '.taobao.com/'){
			    $shop_click_url='http://s.click.taobao.com/t_9?p=mm_'.MYUSERPID.'_0_0&l='.urlencode('http://shop' . $sid . '.taobao.com/').'&u='.$_SESSION['dduserid'];
				$shop[0]['fanxianlv']=1;
				$butie=1;
			}
		}
		if ($shoptype == "C") {
			$shop[0]['onerror'] = "images/tbdp.gif";
			if (is_array($shoppic)) {
				$shop[0]['pic'] = $shop[0]['onerror'];
			}
		}
		$shop[0]['gourl'] = "goto.php?act=shop&url=" . urlencode(base64_encode($shop_click_url)) . "&pic=" . base64_encode($shop[0]['pic']) . "&fan=" .$shop[0]['fanxianlv']. "&name=" . urlencode($shop[0]['title']) . "&sid=" . $shop[0]['sid'];
	}
}

$tag="shoplist";

include_once 'comm/seo.php';
//引入模版文件
include 'template/'.MOBAN.'/shoplist.tpl.php';
mysql_close($cfg_link);
?>